<script>
import {
  findComponentDownward
} from '@admin/utils/index'
// 由一个组件，向下找到最近的指定组件-根据gridKey
function findGridComponentDownward(context, componentName) {
  const childrens = context.$children
  let children = null

  if (childrens.length) {
    for (const child of childrens) {
      const name = child.gridKey

      if (name === componentName) {
        children = child
        break
      } else {
        children = findGridComponentDownward(child, componentName)
        if (children) break
      }
    }
  }
  return children
}
export default {
  data() {
    return {
      tplInstance: null,
      tableInstance: null,
      treeInstance: null,
      filterInstance: null,
      operateInstance: null
    }
  },
  computed: {
    currentTree: function() {
      return this.$store.state.page.currentTree
    }
  },
  mounted: function() {

  },
  methods: {
    getGridInstanceByName(gridKey) {
      return findGridComponentDownward(this, gridKey)
    },
    // 获取模板实例
    getTplInstance() {
      this.tplInstance = this.$refs.tpl
      return this.tplInstance
    },
    // 获取模板keyField
    getTplKeyField() {
      return this.getTplInstance().keyField
    },
    // 表格刷新
    refreshTable() {
      const tplInstacne = this.getTplInstance()
      tplInstacne && tplInstacne.getList && tplInstacne.getList()
    },
    // banner刷新
    refreshBanner() {
      const tplInstacne = this.getTplInstance()
      tplInstacne && tplInstacne.showBanner && tplInstacne.getBannerData()
    },
    // 树刷新
    // setNode: 保留原树节点
    // setScroll: 定位到原scroll，默认为0
    // setNewNode: 选中新增的节点
    refreshTree(setNode = true, setScroll = false, setNewNode = false) {
      const tplInstacne = this.getTplInstance()
      const scrollBar = this.getTreeInstance().getScrollBar()
      const scrollTop = scrollBar.$el.querySelector('.el-scrollbar__wrap').scrollTop
      const oldFlatNodes = tplInstacne.flatData
      tplInstacne && tplInstacne.getTreeData && tplInstacne.getTreeData().then(res => {
        if (setNode && window.$CD.treeId) {
          const currentTree = this.getTreeInstance().getCurrentTree()
          setTimeout(() => {
            currentTree.setCurrentKey(window.$CD.treeId)
          })
        }
        if (setScroll) {
          scrollBar.$el.querySelector('.el-scrollbar__wrap').scrollTop = scrollTop
        }
        if (setNewNode) {
          const newFlatNodes = tplInstacne.flatData
          const newNode = newFlatNodes.filter(i => !oldFlatNodes.map(ii => ii.id).includes(i.id))
          if (newNode && newNode.length) {
            console.log(newNode)
            window.$CD.treeId = newNode[0].id
            this.getTreeInstance().refreshRightById(window.$CD.treeId)
          }
        }
      })
    },
    // 获取筛选条件
    getFilterData() {
      const filterInstance = this.getFilterInstance()
      return filterInstance ? filterInstance.localQuery : {}
    },
    // 获取表格实例
    getTableInstance() {
      // return this.tableInstance || (this.tableInstance = findComponentDownward(this, 'LbTable'))
      return this.tableInstance || (this.tableInstance = this.$refs.tpl.$refs.lbTable)
    },
    // 获取表数据
    getTableData() {
      const tableInstance = this.getTableInstance()
      return tableInstance ? tableInstance.data : []
    },
    // 获取表数据
    getTableInfo() {
      const tableInstance = this.getTableInstance()
      return tableInstance ? tableInstance.tableInfo : {}
    },
    setTableMaxHeight() {
      const tableInstance = this.getTableInstance()
      tableInstance && tableInstance.setTableHeight()
    },
    // 获取当前选中行
    getCurrentRow() {
      const tableInstance = this.getTableInstance()
      return tableInstance ? tableInstance.currentRow : null
    },
    // 获取复选选中行
    getSelectionRows() {
      const tableInstance = this.getTableInstance()
      console.log(tableInstance);
      return tableInstance ? tableInstance.multipleSelection : []
    },
    // 获取左侧树实例
    getTreeInstance() {
      this.treeInstance = findComponentDownward(this, 'CustomTree')
      return this.treeInstance
    },
    // 获取筛选组件实例
    getFilterInstance() {
      return this.filterInstance || (this.filterInstance = findComponentDownward(this, 'TableFilter'))
    },
    // 获取操作按钮组件实例
    getOperateInstance() {
      return this.operateInstance || (this.operateInstance = findComponentDownward(this, 'TableOperate'))
    },
    // 获取表单页面的表单
    getFormData() {
      return this.getTplInstance().getFormData()
    },
    // 暂存表单页面
    saveFormData(validate = false) {
      return this.getTplInstance().formZanSave(validate)
    },
    // 提交表单页面
    handleSubmit(options = {}) {
      return this.getTplInstance().handleSubmit(options)
    }
  }
}
</script>
